#include <iostream>
#include <memory>

using std::cout;
using std::endl;


template <class T>
class Vector
{
public:
    typedef T *iterator;

    Vector()
        : _start(nullptr)
        , _finish(nullptr)
        , _end_of_storage(nullptr)
    {
    }

    ~Vector();
    void push_back(const T &t);
    void pop_back();

    iterator begin()
    {
        return _start;
    }

    iterator end()
    {
        return _finish;
    }

    int size() const
    {
        return _finish - _start;
    }

    int capacity() const
    {
        return _end_of_storage - _start;
    }
private:
    void reallocator();
private:
    static std::allocator<T> _alloc;
    T *_start;
    T *_finish;
    T *_end_of_storage;
};

template <class T>
std::allocator<T> Vector<T>::_alloc;

template<class T>
Vector<T>::~Vector()
{
    if (_start)
    {
        while (_finish != _start)
        {
            _alloc.destroy(--_finish);//对象的销毁
        }
        _alloc.deallocate(_start, capacity());//空间的释放
    }
}

template <class T>
void Vector<T>::push_back(const T &t)
{
    if (size() == capacity())
    {
        reallocator();//当size() == capacity(),需要扩容
    }
    if (size() < capacity())
    {
        _alloc.construct(_finish++, t);
    }
}
template <class T>
void Vector<T>::pop_back()
{
    if (size() > 0)
    {
        _alloc.destroy(--_finish);
    }
}

template <class T>
void Vector<T>::reallocator()
{
    int oldCapacity = capacity();
    int newCapacity = 2 * oldCapacity > 0 ? 2 * oldCapacity : 1;

    T *ptmp = _alloc.allocate(newCapacity);//开辟空间，该空间上是没有对象
    if (_start)
    {
        /* copy(_start, _finish, ptmp); */
        std::uninitialized_copy(_start, _finish, ptmp);
        while (_finish != _start)
        {
            _alloc.destroy(--_finish);
        }
        _alloc.deallocate(_start, oldCapacity);
    }
    _start = ptmp;
    _finish = _start + oldCapacity;
    _end_of_storage = _start + newCapacity;
}

template <class Container>
void display(const Container &vec)
{
    cout << "vec.size() = " << vec.size() << endl
        << "vec.capacity() = " << vec.capacity() << endl;
}

void test()
{
    Vector<int> numbers;
    display(numbers);

    cout << endl;
    numbers.push_back(1);
    display(numbers);

    cout << endl;
    numbers.push_back(2);
    display(numbers);

    cout << endl;
    numbers.push_back(3);
    display(numbers);

    cout << endl;
    numbers.push_back(4);
    display(numbers);

    cout << endl;
    numbers.push_back(5);
    display(numbers);

    cout << endl;
    for (auto &elem : numbers)
    {
        cout << elem << " ";
    }
    cout << endl;;
}
int main(int argc, char **argv)
{
    test();
    return 0;
}







#include <memory>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

template<typename T>
class Vector{
public:
    Vector()
    :_start(nullptr)
    ,_finish(nullptr)
    ,_end_of_storage(nullptr)
    {
        cout << "Vector()" << endl;
    }

    //delete步骤：
    //01 先析构
    //02 再operator delete
    ~Vector(){
        cout << "~Vector()" << endl;
        //销毁对象destory
        if(_start != _finish){
            _alloc.destroy(--_finish);
        }

        //释放空间dellocate
        if(_start){
            _alloc.deallocate(_start,capacity());
        }
    }

    void push_back(const T & rhs){
        cout << "push_back(const T &)" << endl;
        if(size() >= capacity()){
            reallocate();//扩容
        }
        _alloc.construct(_finish++,rhs);
    }

    void pop_back(){
        cout << "pop_back()" << endl;
        if(size() > 0){
            _alloc.destroy(--_finish);
        }
    }

    int size() const {
        return _finish - _start;
    }

    int capacity() const{
        return _end_of_storage - _start;
    }
private:
    //new步骤：
    //01 operator new
    //02 调用构造函数进行初始化
    //03 指针指向该地址
    void reallocate(){
        cout << "reallocate()" << endl;
        //两倍扩容
        int new_capacity = (capacity() == 0) ? 1 : capacity() * 2;

        //复制
        T * _new_start = _alloc.allocate(new_capacity);
        T * _new_finish = _new_start;
        for(T * p = _start; p != _finish; ++p){
            _alloc.construct(_new_finish++, *p);
            _alloc.destroy(p);
        }

        //释放旧内存
        _alloc.deallocate(_start,capacity());

        //修改指针指向
        _start = _new_start;
        _finish = _new_finish;
        _end_of_storage = _new_start + new_capacity;
    }
private:
    static std::allocator<T> _alloc;
    T * _start;
    T * _finish;
    T * _end_of_storage;
};
template<typename T>
allocator<T> Vector<T>::_alloc;

void test(){
    Vector<int> vec;
    cout << vec.size() << endl;
    cout << vec.capacity() << endl;
    vec.push_back(1);
    cout << vec.size() << endl;
    cout << vec.capacity() << endl;
    vec.push_back(2);
    cout << vec.size() << endl;
    cout << vec.capacity() << endl;
    vec.push_back(3);
    cout << vec.size() << endl;
    cout << vec.capacity() << endl;
    vec.push_back(4);
    cout << vec.size() << endl;
    cout << vec.capacity() << endl;
    vec.push_back(5);
    cout << vec.size() << endl;
    cout << vec.capacity() << endl;

    vec.pop_back();
    cout << vec.size() << endl;
    cout << vec.capacity() << endl;
}

int main()
{
    test();
    return 0;
}

